package com.bookstore.services.Impl;


import com.bookstore.mappers.OrderItemMapper;
import com.bookstore.mappers.OrderMapper;
import com.bookstore.pojo.*;
import com.bookstore.services.BookService;
import com.bookstore.services.OrderService;
import com.bookstore.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    BookService bookService=new BookServiceImpl();
    @Override
    public String checkout(Cart cart, User user) {
        SqlSession sqlSession = null;
        String orderId="";//订单号
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            OrderItemMapper mapper=sqlSession.getMapper(OrderItemMapper.class);
            OrderMapper mapper1 = sqlSession.getMapper(OrderMapper.class);
            //封装订单对象
            Order order = new Order();
            order.setCreateDate(new Date());
            //orderId运用算法生成
            long millis = System.currentTimeMillis();//生成时间戳
            orderId = millis+""+user.getId();
            order.setOrderId(orderId);
            order.setTotalPrince(cart.getAmount());
            order.setStatus(0);
            order.setUserId(user.getId());
            //封装订单项对象
            List<CartItem> allItems = cart.getAllItems();
            List<OrderItem> orderItems=new ArrayList<>();
            for (CartItem cartItem:allItems) {
                OrderItem orderItem=new OrderItem(cartItem.getBook().getName()
                        ,cartItem.getCount(),
                        cartItem.getTotalPrice(),
                        cartItem.getBook().getPrice(),
                        orderId);
                orderItems.add(orderItem);
            }
            System.out.println(orderItems);
            //保存订单
            mapper1.insert(order);
            //保存订单项
            for (OrderItem orderItem:orderItems) {
                mapper.saveOrderItem(orderItem);
                System.out.println(orderItem);
            }
            //修改相应库存,图书库存信息
            for (CartItem cartItem:allItems) {
                //获取详细信息,将从页面获取的图书从数据库查询并跟新数据内容
                Book book = cartItem.getBook();
                Book one = bookService.getOne(book);
                Integer count = cartItem.getCount();
                System.out.println("订单项的数目:"+count);
                book.setStock(one.getStock()-count);
                book.setSales(one.getSales()+count);
                System.out.println("checkout:"+one);
                bookService.update(one);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
        return orderId;
    }

    @Override
    public boolean updateStatus(String orderId, String status) {
        SqlSession sqlSession= SqlSessionUtil.getSqlSession();
        OrderMapper orderMapper=sqlSession.getMapper(OrderMapper.class);
        Order order=new Order();
        order.setOrderId(orderId);
        int status1 = 0;
        try {
            status1 = Integer.parseInt(status);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        order.setStatus(status1);
        return orderMapper.updateStatus(order)>0;
    }

    @Override
    public List<Order> getAllOrder() {
        SqlSession sqlSession= null;
        OrderMapper orderMapper= null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            orderMapper = sqlSession.getMapper(OrderMapper.class);
            return orderMapper.getOrderList();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        return null;
    }

    @Override
    public List<Order> getMyOrders(Integer userId) {
        SqlSession sqlSession= null;
        List<Order> orders = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            OrderMapper orderMapper=sqlSession.getMapper(OrderMapper.class);
            orders = orderMapper.getOrderByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        return orders;
    }

    @Override
    public OrderItem getMyOrder(String orderId) {
        SqlSession sqlSession= null;
        OrderItem orderItem = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            OrderItemMapper mapper=sqlSession.getMapper(OrderItemMapper.class);
            orderItem = mapper.selectByPrimaryKey(orderId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        return orderItem;
    }
}
